package com.zerdoor.service.impl;

import com.zerdoor.dao.ItemAppraiseDao;
import com.zerdoor.service.ICommonPayService;
import com.zerdoor.service.ItemAppraiseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zhongjieyu on 2015/1/22.
 */
@Service
public class ItemAppraiseServiceImpl implements ItemAppraiseService {

    @Autowired
    private ItemAppraiseDao itemAppraiseDao;

    @Autowired
    private ICommonPayService commonPayService;

    @Override
    public void addItemAppraise(Map param) throws Exception {

        //根据合同ID查询合同双方userID
        Map map = itemAppraiseDao.findBothPartiesUserIdByContractId(String.valueOf(param.get("contract_id")));

        //等于空表示传入的信息不正确，或该合同不能进行评价
        if(map != null){

            //1表示委托方
            if("1".equals(String.valueOf(param.get("type")))){

                param.put("by_appraiser",map.get("artistUserId"));
                param.put("appraiser",map.get("baileUserId"));

            }else{
                param.put("appraiser",map.get("artistUserId"));
                param.put("by_appraiser",map.get("baileUserId"));

            }
            //查询评价是否已经存在
            List<Map> appList = itemAppraiseDao.findItemAppraiseByBothPartiesUserId(param);

            //等于空表示不存在，那么可以进行评价新增
            if(appList == null || appList.size() == 0){
                //新增评价信息
                itemAppraiseDao.addItemAppraise(param);

            }

            //查询该项目下未评论的数量
            int num = itemAppraiseDao.findSurplusAppraiseNumByItemId(String.valueOf(map.get("item_id")));

            //如果评价数量为0，那么表示该项目所有都评价了,那么就改变项目状态
            if(num == 0){
                //改变项目状态为完成
                itemAppraiseDao.updateCurrentProgressByTaskId(String.valueOf(map.get("item_id")));

                int artistUserId = Integer.parseInt(String.valueOf( map.get("artistUserId")));
                int baileUserId = Integer.parseInt(String.valueOf( map.get("baileUserId")));

                //维护用户双方交易量
                commonPayService.updateUserTradingNumByUserId(artistUserId);
                commonPayService.updateUserTradingNumByUserId(baileUserId);
            }

        }

    }

    @Override
    public Map findItemAppraiseByContractId(int contractId, int type) throws Exception {


        Map param = new HashMap();
        Map data = new HashMap();
        //根据合同ID查询合同双方userID
        Map map = itemAppraiseDao.findBothPartiesUserIdByContractId(String.valueOf(contractId));

        //等于空表示传入的信息不正确，或该合同不能进行评价
        if(map != null){


            param.put("contract_id",contractId);
            //1表示委托方
            if(1 == type){

                param.put("by_appraiser",map.get("artistUserId"));
                param.put("appraiser",map.get("baileUserId"));

            }else{
                param.put("appraiser",map.get("artistUserId"));
                param.put("by_appraiser",map.get("baileUserId"));

            }
            //查询评价是否已经存在
            List<Map> appList = itemAppraiseDao.findItemAppraiseByBothPartiesUserId(param);

            //等于空表示不存在，那么可以进行评价新增
            if(appList != null && appList.size() != 0){

                data = appList.get(0);

            }
            data.put("title",map.get("title"));

        }

        return data;
    }



    @Override
    public List<Map<String, Object>> findWorksAppraiseByWorksId(Map<String, String> param) throws Exception {

        return itemAppraiseDao.findWorksAppraiseByWorksId(param);
    }

    @Override
    public void addWorksAppraise(Map<String, String> param) throws Exception {


        String count = itemAppraiseDao.findOrderWorksByUserId(param);
        //如果为空，表示该用户没有购买过该作品，无权限评论
        if("".equals(count) || count == null){

            return;
        }

        Map map = itemAppraiseDao.findWorksAppraiseByUserId(param);
        //如果map不等于空表示已经评价过了，不能再重复评价
        if(map != null){
            return;
        }

        itemAppraiseDao.addWorksAppraise(param);

    }

    @Override
    public Map findWorksAppraiseByUserId(Map<String, String> param) throws Exception {


        return itemAppraiseDao.findWorksAppraiseByUserId(param);
    }
}
